<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   this($)*/

!defined('DYHB_PATH') && exit;

// 导入接口
Package::importI('ITemplateAutoloadableProcessor','TEMPLATE');
Package::importI('ITemplateObjCompiler','TEMPLATE');

abstract class TemplateGtkGladeWidgetCompilerBase extends TemplateObjCompilerBase implements ITemplateAutoloadableProcessor,ITemplateObjCompiler{

	public function compile(TemplateObj $oObject){
		A::INSTANCE($oObject,'TemplateGtkGladeWidget');
		$sCompiled=$this->compileCreateWidget_($oObject);// 创建窗体代码
		$oIterator=$oObject->createTemplateObjIterator();// 子窗体
		$oIterator->first();
		while(!$oIterator->isDone()){
			$oChild=$oIterator->current();
			$oPacking=$oChild->getPacking();
			$oChildWidget=$oChild->getWidget();
			A::INSTANCE($oPacking,'TemplateGtkGladeWidgetPacking');
			A::INSTANCE($oChildWidget,'TemplateGtkGladeWidget');
			$sCompiled.=$this->compileChildWidgetPacking_($oObject,$oChildWidget,$oPacking);
			$oIterator->next();
		}
		$oObject->setCompiled((string)$sCompiled);
	}

	abstract protected function compileCreateWidget_(TemplateGtkGladeWidget $oObject);

	abstract protected function compileChildWidgetPacking_(TemplateGtkGladeWidget $oParentWidget,TemplateGtkGladeWidget $oChildWidget,TemplateGtkGladeWidgetPacking $oPacking);

	protected function setStringInt_(TemplateGtkGladeWidget $oObject,&$sCompiled,$sPropertyName,$bString=true,$sSetMethodName=null){
		A::STRING($sCompiled);
		if($sSetMethodName===null){$sSetMethodName='set_'.$sPropertyName;}
		A::STRING($sSetMethodName);
		if(($sValue=$oObject->getProperty($sPropertyName))===null){return;}
		if(!$bString){// 常量
			$sValue=str_replace('GTK_','Gtk::',$sValue);
			$sValue=str_replace('GDK_','Gdk::',$sValue);
			$sValue=str_replace('PANGO_','Pango::',$sValue);
		}
		else{$sValue="'{$sValue}'";}
		$sVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oObject);
		$sCompiled.= "\t\t{$sVariableName}->{$sSetMethodName}({$sValue});";
	}

	protected function setBool_(TemplateGtkGladeWidget $oObject,&$sCompiled,$sPropertyName,$sSetMethodName=null){
		A::STRING($sCompiled);
		if($sSetMethodName===null){$sSetMethodName='set_'.$sPropertyName;}
		A::STRING($sSetMethodName);
		$bValue=TemplateGtkGladeObj::nodeTextToValue($oObject->getProperty($sPropertyName));
		if($bValue===null){return;}
		$sValue=$bValue?'true':'false';
		$sVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oObject);
		$sCompiled.="\t\t{$sVariableName}->{$sSetMethodName}({$sValue});";
	}

	protected function getProperty_(TemplateGtkGladeWidget $oObject,$sName,$sDefault){
		return (($sValue=$oObject->getProperty($sName))===null)?$sDefault:$sValue;
	}

	protected function createChildWidget_(TemplateGtkGladeWidget $oChildWidget){
		$sChildWidgetVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oChildWidget);
		$sWidgetId=$oChildWidget->getWidgetId();
		return "{$sChildWidgetVariableName}=self::getWidget('{$sWidgetId}',\$arrObjectForSignal);";
	}

	protected function compileCallback_($sSource){
		$arrHandle=explode('::',$sSource);
		foreach($arrHandle as &$sItem){
			if(preg_match('/^[\w_]+$/',$sItem)){$sItem="'{$sItem}'";}
		}
		$n=count($arrHandle);
		if($n==1){$sHandle=$arrHandle[0];}// 全局函数
		else if($n==2){$sHandle="array({$arrHandle[0]},{$arrHandle[1]})";}// 类方法
		else{G::E(G::L('意外的 Gtk Signal Handle格式“%s”','Dyhb',null,$arrHandle['function']));}// 意外
		return $sHandle;
	}

}
